package terminal;

import java.util.ArrayList;
import java.util.List;

import gui.BattleView;
import gui.GUICore;
import gui.HandViewPanel;
import gui.GUIConstants.ViewState;

import org.apache.xmlrpc.XmlRpcException;

import audio.AePlayWave;

import xmlrpc.client.ServerCommunicator;
import card.*;
import card.buildings.*;
import card.creatures.*;

import command.Command;
import command.CommandExecutor;
import command.CommandConstants.CommandType;

import core.Battle;
import core.Core;
import core.MapUtil;
import core.Player;
import core.TurnController;


/**
 *
 * @author Anton
 * Temporary terminal used for testing.
 * supported commands (not necessarily up to date):
 * /spawn
 * /move cardId tileId (outdated)
 * /battleView
 * /mapView
 * /player
 * /setPlayer Int
 * /turn
 */
public class ExperimentalTerminal implements ITerminal{
	//string with the executed command
	private String executed;


	protected ExperimentalTerminal(){

	}
	public void Execute(String s){
		String[] param = s.split("\\s");

		if(s.charAt(0)=='/'){
			if(param[0].equals("/spawn")){
				CreatureCard g = new MrDebug();
				g.setOwner(Core.getInstance().getCurrentPlayer());
				spawnCreatureCard(g, g.getOwner().getStartPos().x, g.getOwner().getStartPos().y);
				executed = "New card with id "+Core.getInstance().getIdOfCard(g);
			}
			if(param[0].equals("/move")){
				//generates a movecommand and sends it to CommandExecutor
				Command c = new Command(CommandType.move, Integer.parseInt(param[1]), 0, Integer.parseInt(param[2]), 0, 0, 0);
				CommandExecutor.getInstance().executeCommand(c);
				executed = "moving card no. "+param[1]+" to tile no. "+param[2];
			}
			if(param[0].equals("/claim")){
				// sets the owner of a given tile to currentPlayer
				MapUtil.getTile(Integer.parseInt(param[1])).setOwningPlayer(Core.getInstance().getCurrentPlayer());
				executed = "Claimed tile "+param[1];
			}
			if(param[0].equals("/battleView")){
				//enters battleview
				CreatureCard card = new MrDebug();
				card.setOwner(Core.getInstance().getPlayers().get(0));
				spawnCreatureCard(card, 2, 3);
				card = new MrDebug();
				card.setOwner(Core.getInstance().getPlayers().get(0));
				spawnCreatureCard(card, 2, 3);
				card = new MrDebug();
				card.setOwner(Core.getInstance().getPlayers().get(1));
				spawnCreatureCard(card, 3, 3);
				Battle.getInstance().initiateBattle(Core.getInstance().getMap()[2][3], Core.getInstance().getMap()[3][3]);
				GUICore.getInstance().currentView=ViewState.BATTLE;
				BattleView.getInstance().battleStarted();
				GUICore.getInstance().updateAll();
				executed = "Entering battleView";
			}
			if(param[0].equals("/mapView")){
				//enters mapview
				GUICore.getInstance().currentView=ViewState.MAP;
				GUICore.getInstance().updateAll();
				executed = "Entering mapView";
			}
			if(param[0].equals("/player")){
				executed = "Checking current player. It is player no." + Core.getInstance().getCurrentPlayer().getPlayerId();
			}
			if(param[0].equals("/setCustomHand")){
				setCustomHand();
			}
			if(param[0].equals("/setPlayer")){
				Core.getInstance().setThisPlayer(Core.getInstance().getPlayers().get(Integer.parseInt(param[1])));
				executed = "Player id changed";
			}
			
			if(param[0].equals("/playAudio")){
				AePlayWave temp = new AePlayWave(param[1]);
				temp.run();
				executed = "Playing audiofile: " + param[1];
			}
			
			if(param[0].equals("/turn")){
				TurnController.getInstance().changeTurn();
				//HandViewPanel.getInstance().setOwningPlayer(Core.getInstance().getCurrentPlayer());
				executed = "Changing to the next player." + Core.getInstance().getCurrentPlayer().getPlayerId();
			}
			if(param[0].equals("/send")){
				String reply = "";
				ServerCommunicator sc = new ServerCommunicator();
				String ss = param[1].trim();
				try {
					reply = sc.sendCommandString(ss);
				} catch (XmlRpcException e) {
					e.printStackTrace();
				}
				executed = reply;
			}

		}

	}
	/**
	 * REMEMBER! set ownership of card before spawning
	 * @param card
	 * @param x
	 * @param y
	 */
	private void spawnCreatureCard(CreatureCard card, int x, int y){
		//tell card it's being played
		card.playCard();
		//place card on the map
		Core.getInstance().getMap()[x][y].addCreature(card);
		//tell core that the card exist
		Core.getInstance().addIngameCard(card);
	}

	public String getOutputText(){
		return executed;

	}
	private void setCustomHand(){
		List<Card> newHand = new ArrayList<Card>();
		newHand.add(new SwampTroll());
		newHand.add(new Halfling());
		newHand.add(new Skeleton());
		newHand.add(new MarketPlace());
		Player player = Core.getInstance().getCurrentPlayer();

		player.getHand().clear();
		
		for(Card c : newHand){
			Core.getInstance().addIngameCard(c);
			c.setOwner(player);
			player.getHand().add(c);
		}
		HandViewPanel.getInstance().update(null, null);
	}

}
